﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using System.Collections;
using System.Xml.Linq;
using System.Xml;
using System.Data.Services.Client;
using System.IO;
using Ivvy_Silverlight.i7SR;
using System.Windows.Data;
   

namespace Ivvy_Silverlight
{
    public partial class Activitywindow : ChildWindow
    {
        //IvvyDomainContext dbconn = new IvvyDomainContext();
        //string Uri = "http://localhost:11080/i7s.svc/";
        //WebClient webclient;

        string Uri = "http://localhost:11080/i7s.svc/";
        Int32 rowIndex = 0;
        public i7e svc = null;
        public i7e svc2 = null;
        public i7e svc4 = null;
        public i7e ctxnew = null;
        public i7e ctxnew1 = null;
        public i7e ctxnew4 = null;
        public Activitywindow()
        {
            loadactionType();

            loadactionconfigured();
            loadfieldtype();
           
            InitializeComponent();
        }

        

        

        void test_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //MessageBox.Show(e.Result.ToString());
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {


            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
       private void  loadfieldtype()
         {
             svc = new i7e(new System.Uri(Uri));
             DataServiceCollection<Field> FieldBindingCollection = new DataServiceCollection<Field>();
             FieldBindingCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(FieldBindingCollection_LoadCompleted);
           
             DataServiceQuery<Field> query = svc.Fields;

             FieldBindingCollection.LoadAsync(query);
        
        
        }

       void FieldBindingCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
       {
           var binding = (DataServiceCollection<Field>)sender;

           if (e.Error == null)
           {
               if (binding.Continuation != null)
               {
                   binding.LoadNextPartialSetAsync();
               }
               else
               {
                   FieldAction.ItemsSource = binding;
               }
           }
           else
           {
               ChildWindow cw = new ChildWindow();
               cw.Content = string.Format("An error has occured: {0}", e.Error.Message);
               cw.Show();
           }
       }

        private void loadactionType()
        {
            svc = new i7e(new System.Uri(Uri));
            DataServiceCollection<Entry> customerBindingCollection = new DataServiceCollection<Entry>();
            customerBindingCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(customerBindingCollection_LoadCompleted);
            var queryresult = from Entries in svc.Entries
                              where Entries.GUID == ""
                              select Entries;

            DataServiceQuery<Entry> query = (DataServiceQuery<Entry>)queryresult;

            customerBindingCollection.LoadAsync(query);


        }

        void customerBindingCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var binding = (DataServiceCollection<Entry>)sender;

            if (e.Error == null)
            {
                if (binding.Continuation != null)
                {
                    binding.LoadNextPartialSetAsync();
                }
                else
                {
                    EntryActionType.ItemsSource = binding;
                }
            }
            else
            {
                ChildWindow cw = new ChildWindow();
                cw.Content = string.Format("An error has occured: {0}", e.Error.Message);
                cw.Show();
            }
        }

        private void loadactionconfigured()
        {
            svc = new i7e(new System.Uri(Uri));
            DataServiceCollection<Entry> entrynullbindingCollection = new DataServiceCollection<Entry>();
            entrynullbindingCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(entrynullbindingCollection_LoadCompleted);
            var queryresult = from Entries in svc.Entries
                              where Entries.GUID != ""
                              select Entries;

            DataServiceQuery<Entry> query = (DataServiceQuery<Entry>)queryresult;

            entrynullbindingCollection.LoadAsync(query);
        }

        void entrynullbindingCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var binding = (DataServiceCollection<Entry>)sender;
             

            if (e.Error == null)
            {
                if (binding.Continuation != null)
                {
                    binding.LoadNextPartialSetAsync();
                }
                else
                {
                    EntryActionConfigured.ItemsSource = binding;
                }
            }
            else
            {
                ChildWindow cw = new ChildWindow();
                cw.Content = string.Format("An error has occured: {0}", e.Error.Message);
                cw.Show();
            }
        }

       

        void entryactionbindingCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var binding = (DataServiceCollection<Entry>)sender;
            ctxnew = new i7e(new Uri(Uri));

            if (e.Error == null)
            {
                if (binding.Continuation != null)
                {
                    binding.LoadNextPartialSetAsync();
                }
                else
                {

                    Entry obj = new Entry();

                    obj = binding.Select(en => en).Single();




                       obj.GUID = "";
                       ctxnew.AttachTo("Entries", obj);  
                    ctxnew.UpdateObject(obj);
                    ctxnew.BeginSaveChanges(BeginSaveChangesCallback2, ctxnew);
                }
            }
            else
            {
                ChildWindow cw = new ChildWindow();
                cw.Content = string.Format("An error has occured: {0}", e.Error.Message);
                cw.Show();
            };
        }
        private void BeginSaveChangesCallback2(IAsyncResult result)
        {
            if (result.AsyncState is i7e)
                ((i7e)result.AsyncState).EndSaveChanges(result);
            loadactionType();

            loadactionconfigured();
         
        }
        private void BtnType_Click(object sender, RoutedEventArgs e)
        {
            if (EntryActionConfigured.ActiveCell != null)
            {

                Int32 rowIndexvalue = ((Ivvy_Silverlight.i7SR.Entry)(EntryActionConfigured.ActiveCell.Row.Data)).Entry_ID;


                svc = new i7e(new System.Uri(Uri));
                DataServiceCollection<Entry> entryactionbindingCollection = new DataServiceCollection<Entry>();
                entryactionbindingCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(entryactionbindingCollection_LoadCompleted);
                var queryresult = from en in svc.Entries
                                  where en.Entry_ID == Convert.ToInt32(rowIndexvalue)
                                  select en;

                DataServiceQuery<Entry> query = (DataServiceQuery<Entry>)queryresult;

                entryactionbindingCollection.LoadAsync(query);
            }
            else
            {
                MessageBox.Show("Please Select Action  Type.");
            }
         
        }
        public void SupplierLoadComplete(IAsyncResult result)
        {
            svc.EndLoadProperty(result);
        }

        private void Moveconfiguration_Click(object sender, RoutedEventArgs e)
        {
            svc = new i7e(new Uri(Uri));
            if (EntryActionConfigured.ActiveCell != null)
            {
                rowIndex = ((Ivvy_Silverlight.i7SR.Entry)(EntryActionConfigured.ActiveCell.Row.Data)).Entry_ID;

                if (FieldAction.ActiveCell != null)
                {
                    var q = (from cells in FieldAction.SelectionSettings.SelectedCells
                             select cells.Row.Data).ToList();



                    for (int i = 0; i < q.Count; i++)
                    {
                        Entry_Field obj = new Entry_Field();

                        obj.Entry_ID = rowIndex;

                        obj.Field_ID = Convert.ToInt32(((Ivvy_Silverlight.i7SR.Field)(q[i])).Field_ID);
                        obj.Caption = ((Ivvy_Silverlight.i7SR.Field)((q)[i])).Field1.ToString();
                        obj.Index = i;
                        obj.GUID = Guid.NewGuid().ToString();
                        svc.AddObject("Entry_Field", obj); 
                    }
                    svc.BeginSaveChanges(BeginSaveChangesCallback, svc);
                }
                else
                {
                    MessageBox.Show("Please Select Fields Avaiable for selection.");
                }
            }

            else
            {
                MessageBox.Show("Please Select action Type Configured Values.");

            }

            
           }

     private void  loadconfiguration(Int32 rowindex)
      {
     

            svc = new i7e(new System.Uri(Uri));
            DataServiceCollection<Entry_Field> Entry_FieldBindingCollection = new DataServiceCollection<Entry_Field>();
            Entry_FieldBindingCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(Entry_FieldBindingCollection_LoadCompleted);
            var queryresult = from Entry_Field in svc.Entry_Field
                              where Entry_Field.Entry_ID == rowindex && Entry_Field.Caption != null && Entry_Field.Caption !=""
                              select Entry_Field;

            DataServiceQuery<Entry_Field> query = (DataServiceQuery<Entry_Field>)queryresult;

         Entry_FieldBindingCollection.LoadAsync(query);
         
      
      }

        void Entry_FieldBindingCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var binding = (DataServiceCollection<Entry_Field>)sender;


            if (e.Error == null)
            {
                if (binding.Continuation != null)
                {
                    binding.LoadNextPartialSetAsync();
                }
                else
                {
                    var m = binding.ToList(); 
                    Configuration.ItemsSource = binding;
                    
                }
            }
            else
            {
                ChildWindow cw = new ChildWindow();
                cw.Content = string.Format("An error has occured: {0}", e.Error.Message);
                cw.Show();
            }
        }
        private void BeginSaveChangesCallback(IAsyncResult result)
        {
            if (result.AsyncState is i7e)
                ((i7e)result.AsyncState).EndSaveChanges(result);
            loadactionType();

            loadactionconfigured();
            loadfieldtype();
            loadconfiguration(rowIndex);
        }

        private void savedata_Click(object sender, RoutedEventArgs e)
        {
            svc = new i7e(new Uri(Uri));
        Int32 countrows = Convert.ToInt32((((System.Collections.ObjectModel.Collection<Ivvy_Silverlight.i7SR.Entry_Field>)(((System.Collections.ObjectModel.ObservableCollection<Ivvy_Silverlight.i7SR.Entry_Field>)(Configuration.ItemsSource))))).Count);

        for (int i = 0; i < countrows; i++)
        {
            DataGrid dg = this.Configuration;
            dg.SelectedIndex = i; 
            TextBlock txt = dg.Columns[0].GetCellContent(dg.SelectedItem) as TextBlock;
            CheckBox Check1 = dg.Columns[1].GetCellContent(dg.SelectedItem) as CheckBox;
            CheckBox Check2 = dg.Columns[2].GetCellContent(dg.SelectedItem) as CheckBox;
            CheckBox Check3 = dg.Columns[3].GetCellContent(dg.SelectedItem) as CheckBox;
            CheckBox Check4 = dg.Columns[4].GetCellContent(dg.SelectedItem) as CheckBox;
            TextBlock txt1 = dg.Columns[5].GetCellContent(dg.SelectedItem) as TextBlock;
            TextBlock txt2 = dg.Columns[6].GetCellContent(dg.SelectedItem) as TextBlock;

            svc = new i7e(new System.Uri(Uri));
            DataServiceCollection<Field_Details> entryFieledconfiguraionCollection = new DataServiceCollection<Field_Details>();
            entryFieledconfiguraionCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(entryFieledconfiguraionCollection_LoadCompleted);
            
            var queryresult = from en in svc.Field_Details
                              //where  en.Entry_Field_ID == Convert.ToInt32(txt2.Text)  
                              select en;

            DataServiceQuery<Field_Details> query = (DataServiceQuery<Field_Details>)queryresult;

            entryFieledconfiguraionCollection.LoadAsync(query);


               
            if (((System.Windows.Controls.Primitives.ToggleButton)(Check1)).IsChecked == true)
            {
                Field_Details objnew = new Field_Details();

                objnew.Field_ID = Convert.ToInt32(txt1.Text);
                objnew.Index = Convert.ToInt32(Check1.Name);
                objnew.Value = "Yes";
                objnew.Mask_ID =0;
                //objnew.Entry_Field_ID = Convert.ToInt32(txt2.Text);       
                objnew.isDefault = false;
                svc.AddObject("Field_Details", objnew); 
            }

            if (((System.Windows.Controls.Primitives.ToggleButton)(Check2)).IsChecked == true)
            {
                Field_Details objnew = new Field_Details();

                objnew.Field_ID = Convert.ToInt32(txt1.Text);
                objnew.Index = Convert.ToInt32(Check2.Name);
                objnew.Value = "No";
                objnew.Mask_ID = 0;
                //objnew.Entry_Field_ID = Convert.ToInt32(txt2.Text);      
                objnew.isDefault = false;
                svc.AddObject("Field_Details", objnew);
            }

            if (((System.Windows.Controls.Primitives.ToggleButton)(Check3)).IsChecked == true)
            {
                Field_Details objnew = new Field_Details();

                objnew.Field_ID = Convert.ToInt32(txt1.Text);
                objnew.Index = Convert.ToInt32(Check3.Name);
                objnew.Value = "Undefined";
                objnew.Mask_ID = 0;
                //objnew.Entry_Field_ID = Convert.ToInt32(txt2.Text);    
                objnew.isDefault = false;
                svc.AddObject("Field_Details", objnew);
            }
            if (((System.Windows.Controls.Primitives.ToggleButton)(Check4)).IsChecked == true)
            {
                Field_Details objnew = new Field_Details();

                objnew.Field_ID = Convert.ToInt32(txt1.Text);
                objnew.Index = Convert.ToInt32(Check4.Name);
                objnew.Value = "Don't Know";
                objnew.Mask_ID = 0;
                //objnew.Entry_Field_ID = Convert.ToInt32(txt2.Text);   

                objnew.isDefault = false;
                svc.AddObject("Field_Details", objnew);
            }
            
           
            
        }
        svc.BeginSaveChanges(BeginSaveChangesCallback1, svc);  
          
        }

        void entryFieledconfiguraionCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var binding = (DataServiceCollection<Field_Details>)sender;
            ctxnew4 = new i7e(new Uri(Uri));
           
            if (e.Error == null)
            {
                if (binding.Continuation != null)
                {
                    binding.LoadNextPartialSetAsync();
                }
                else
                {

                    Field_Details obj = new Field_Details();
                    foreach (var b in binding)
                    {
                       // obj =Convert.ToInt32(b.Field_Details_ID);
                        ctxnew4.AttachTo("Field_Details", b);
                        ctxnew4.DeleteObject(b);
                      
                    }

                    ctxnew4.BeginSaveChanges(BeginSaveChangesCallback5, ctxnew4);
                                                        
                   // ctxnew.AttachTo("Field_Details", obj);
                   
                }
            }
            else
            {
                ChildWindow cw = new ChildWindow();
                cw.Content = string.Format("An error has occured: {0}", e.Error.Message);
                cw.Show();
            };
        }


        private void BeginSaveChangesCallback5(IAsyncResult result)
        {
            if (result.AsyncState is i7e)
                ((i7e)result.AsyncState).EndSaveChanges(result);



        }
        private void BeginSaveChangesCallback1(IAsyncResult result)
        {
            if (result.AsyncState is i7e)
                ((i7e)result.AsyncState).EndSaveChanges(result);
           

         
        }

        private void BtnTypeConfigured_Click(object sender, RoutedEventArgs e)
        {
            if (EntryActionType.ActiveCell != null)
            {
                Int32 rowIndexval = ((Ivvy_Silverlight.i7SR.Entry)(EntryActionType.ActiveCell.Row.Data)).Entry_ID;


                svc = new i7e(new System.Uri(Uri));
                DataServiceCollection<Entry> entryactionbindingfornullCollection = new DataServiceCollection<Entry>();
                entryactionbindingfornullCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(entryactionbindingfornullCollection_LoadCompleted);
                var queryresult = from en in svc.Entries
                                  where en.Entry_ID == Convert.ToInt32(rowIndexval)
                                  select en;

                DataServiceQuery<Entry> query = (DataServiceQuery<Entry>)queryresult;

                entryactionbindingfornullCollection.LoadAsync(query);
            }
            else
            {
                MessageBox.Show("Please Select Action  Type.");
            }
        }

        void entryactionbindingfornullCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var binding = (DataServiceCollection<Entry>)sender;
            ctxnew = new i7e(new Uri(Uri));

            if (e.Error == null)
            {
                if (binding.Continuation != null)
                {
                    binding.LoadNextPartialSetAsync();
                }
                else
                {

                    Entry obj = new Entry();

                    obj = binding.Select(en => en).Single();




                    obj.GUID = Guid.NewGuid().ToString()  ;
                    ctxnew.AttachTo("Entries", obj);
                    ctxnew.UpdateObject(obj);
                    ctxnew.BeginSaveChanges(BeginSaveChangesCallback3, ctxnew);
                }
            }
            else
            {
                ChildWindow cw = new ChildWindow();
                cw.Content = string.Format("An error has occured: {0}", e.Error.Message);
                cw.Show();
            };
        }

        private void BeginSaveChangesCallback3(IAsyncResult result)
        {
            if (result.AsyncState is i7e)
                ((i7e)result.AsyncState).EndSaveChanges(result);
            loadactionType();

            loadactionconfigured();

        }

        private void Removeconfiguration_Click(object sender, RoutedEventArgs e)
        {
            if (Configuration.SelectedIndex != 0)
            {
                Int32 indexval = Configuration.SelectedIndex;
                DataGrid dg = this.Configuration;
                dg.SelectedIndex = indexval;
                TextBlock txt1 = dg.Columns[6].GetCellContent(dg.SelectedItem) as TextBlock;



                svc = new i7e(new System.Uri(Uri));
                DataServiceCollection<Field_Details> entrycongurationremoveFieldsCollection = new DataServiceCollection<Field_Details>();
                entrycongurationremoveFieldsCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(entrycongurationremoveFielsCollection_LoadCompleted);
                var queryresult1 = from en in svc.Field_Details
                                 // where en.Entry_Field_ID == Convert.ToInt32(txt1.Text)
                                  select en;

                DataServiceQuery<Field_Details> query1 = (DataServiceQuery<Field_Details>)queryresult1;

                entrycongurationremoveFieldsCollection.LoadAsync(query1);

                DataServiceCollection<Entry_Field> entrycongurationremoveCollection = new DataServiceCollection<Entry_Field>();
                entrycongurationremoveCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(entrycongurationremoveCollection_LoadCompleted);
                var queryresult = from en in svc.Entry_Field
                                  where en.Entry_Field_ID == Convert.ToInt32(txt1.Text)
                                  select en;

                DataServiceQuery<Entry_Field> query = (DataServiceQuery<Entry_Field>)queryresult;

                entrycongurationremoveCollection.LoadAsync(query);
            }
            else {
                MessageBox.Show("Please Select Field Configuration.");
            }

        }

        void entrycongurationremoveFielsCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var binding = (DataServiceCollection<Field_Details>)sender;
            svc4 = new i7e(new Uri(Uri));

            if (e.Error == null)
            {
                if (binding.Continuation != null)
                {
                    binding.LoadNextPartialSetAsync();
                }
                else
                {

                    Field_Details obj = new Field_Details();

                    foreach (var b in binding)
                    {
                        // obj =Convert.ToInt32(b.Field_Details_ID);
                        svc4.AttachTo("Field_Details", b);
                        svc4.DeleteObject(b);

                    }

                    svc4.BeginSaveChanges(BeginSaveChangesCallback7, svc4);

                    // ctxnew.AttachTo("Field_Details", obj);

                }
            }
            else
            {
                ChildWindow cw = new ChildWindow();
                cw.Content = string.Format("An error has occured: {0}", e.Error.Message);
                cw.Show();
            };
        }

        void entrycongurationremoveCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var binding = (DataServiceCollection<Entry_Field>)sender;
            svc2 = new i7e(new Uri(Uri));

            if (e.Error == null)
            {
                if (binding.Continuation != null)
                {
                    binding.LoadNextPartialSetAsync();
                }
                else
                {

                    Entry_Field obj = new Entry_Field();
                    
                    foreach (var b in binding)
                    {
                        // obj =Convert.ToInt32(b.Field_Details_ID);
                        svc2.AttachTo("Entry_Field", b);
                        svc2.DeleteObject(b);

                    }

                    svc2.BeginSaveChanges(BeginSaveChangesCallback6, svc2);

                    // ctxnew.AttachTo("Field_Details", obj);

                }
            }
            else
            {
                ChildWindow cw = new ChildWindow();
                cw.Content = string.Format("An error has occured: {0}", e.Error.Message);
                cw.Show();
            };
        }

        private void BeginSaveChangesCallback7(IAsyncResult result)
        {
            if (result.AsyncState is i7e)
                ((i7e)result.AsyncState).EndSaveChanges(result);
          

        }
        private void BeginSaveChangesCallback6(IAsyncResult result)
        {
            if (result.AsyncState is i7e)
                ((i7e)result.AsyncState).EndSaveChanges(result);
            loadconfiguration(rowIndex);

        }
    }
}

